package com.joven.shelltest.utils.delayed;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wzj
 * @Date: 2024/2/18 15:35
 */
@Data
public class Message implements Delayed {

    private int id;
    /** 消息内容 */
    private String body;
    /** 延迟时长，这个是必须的属性因为要按照这个判断延时时长 */
    private long executeTime;

    public Message(int id, String body, long delayTime) {
        this.id = id;
        this.body = body;
        this.executeTime = TimeUnit.NANOSECONDS.convert(delayTime, TimeUnit.MILLISECONDS) + System.nanoTime();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.executeTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed delayed) {
        Message msg = (Message) delayed;
        return Integer.compare(this.id, msg.id);
    }
}
